//
// Created by GYC on 2021/6/1.
//

#include <list>
#include "../../common.h"

struct MapNode {
    int key;
    int val;
    MapNode *next;

    MapNode() : key(0), val(-1), next(nullptr) {}

    MapNode(int key, int val) : key(key), val(val), next(nullptr) {}

    MapNode(int key, int val, MapNode *next) : key(key), val(val), next(next) {}
};

class MyHashMap {

private:
    vector<MapNode*> store;
    int act;
    list<int> ll;

public:
    /** Initialize your data structure here. */
    MyHashMap() {
        act = 769;
        store = vector<MapNode*>(act, new MapNode());
    }

    /** value will always be non-negative. */
    void put(int key, int value) {
        int k = key % act;
        MapNode *p = store[k];
        while (p->next != nullptr) {
            p = p->next;
            if (p->key == key) {
                p->val = value;
                return;
            }
        }
        p->next = new MapNode(key, value);
    }

    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    int get(int key) {
        int k = key % act;
        MapNode *p = store[k];
        while (p->next != nullptr) {
            p = p->next;
            if (p->key == key) {
                return p->val;
            }
        }
        return -1;
    }

    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) {
        int k = key % act;
        MapNode *p = store[k];
        while (p->next != nullptr) {
            auto *curr = p->next;
            if (curr->key == key) {
                p->next = curr->next;
                delete curr;
                return;
            }
            p = p->next;
        }
    }
};

int main() {

    auto *map = new MyHashMap();
    map->put(1, 1);
    map->put(2,2);
    map->get(1);
    map->get(3);
    map->put(2,1);
    map->get(2);
    map->get(2);
    map->remove(2);
    map->get(2);
}